// Game.cs created with MonoDevelop
// User: dream at 15:50 15.05.2008
//
using System;

namespace CheckAndMate
{
	public enum File: int { a = 1, b, c, d, e, f, g, h }; 
		
	public class Game
	{
		public Game(bool def)
		{
			_board = new Piece[8, 8]; //Default initialize 0: None for color, type 
			
			if (def)
			{
				_turnMove = PieceColor.White;
				InitialBoard();
			}
		}
		
		public Piece GetPiece(File f, int r)
		{
			if (r >= 1 && r <= 8 && (int)f >= 1 && (int)f <= 8)  
				return _board[r - 1, (int)f - 1];
			else
				throw new ArgumentOutOfRangeException();
		}
		
		public Piece GetPiece(Square s)
		{
			return _board[s.Rank - 1, (int)s.File - 1];
		}
		
		public void SetPiece(Piece p, Square s)
		{
			_board[s.Rank - 1, (int)s.File - 1] = p;
		}
		
		public bool VerifyMove(Square source, Square dest)
		{
			Piece pSource = GetPiece(source);
			Piece pDest = GetPiece(dest);
			if (source == dest)
				return false;
			if (pSource.Type == PieceType.None)
				return false;
			if (pSource.Color != _turnMove)
				return false;
			if (pSource.Color == pDest.Color)
				return false;
			
			return true;
		}
		
		public PieceColor TurnMove
		{
			get { return _turnMove; }
			set { _turnMove = value;}
		}
		
		private void InitialBoard()
		{	
			for (int file = 0; file <= 7; file++)
			{
				_board[1, file] = new Piece(PieceColor.White, PieceType.Pawn);
				_board[6, file] = new Piece(PieceColor.Black, PieceType.Pawn);
			}
			
			// White
			_board[0, 0] = new Piece(PieceColor.White, PieceType.Rook);
			_board[0, 1] = new Piece(PieceColor.White, PieceType.Knight);
			_board[0, 2] = new Piece(PieceColor.White, PieceType.Bishop);
			_board[0, 3] = new Piece(PieceColor.White, PieceType.Queen);
			_board[0, 4] = new Piece(PieceColor.White, PieceType.King);
			_board[0, 5] = new Piece(PieceColor.White, PieceType.Bishop);
			_board[0, 6] = new Piece(PieceColor.White, PieceType.Knight);
			_board[0, 7] = new Piece(PieceColor.White, PieceType.Rook);
			
			// Black
			_board[7, 0] = new Piece(PieceColor.Black, PieceType.Rook);
			_board[7, 1] = new Piece(PieceColor.Black, PieceType.Knight);
			_board[7, 2] = new Piece(PieceColor.Black, PieceType.Bishop);
			_board[7, 3] = new Piece(PieceColor.Black, PieceType.Queen);
			_board[7, 4] = new Piece(PieceColor.Black, PieceType.King);
			_board[7, 5] = new Piece(PieceColor.Black, PieceType.Bishop);
			_board[7, 6] = new Piece(PieceColor.Black, PieceType.Knight);
			_board[7, 7] = new Piece(PieceColor.Black, PieceType.Rook);	
		}
		
		private Piece[,] _board;
		private PieceColor _turnMove;
	}
}